package cn.t.facade.sku;


import cn.t.constants.ParamConstants;
import cn.t.constants.ResponseError;
import cn.t.converter.sku.CategoryDTOConverter;
import cn.t.core.exception.BizException;
import cn.t.core.facade.FacadeImpl;
import cn.t.core.mode.PageInfoDTO;
import cn.t.core.mode.PageQueryDTO;
import cn.t.dto.sku.CategoryDTO;
import cn.t.model.sku.CategoryDO;
import cn.t.request.CategoryReq;
import cn.t.request.CreateCategoryReq;
import cn.t.service.sku.CategoryService;
import cn.t.vo.sku.CategoryVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 商品类别服务编排
 *
 * @author s
 * @since 2024-06-17
 */
@Slf4j
@Service
@AllArgsConstructor
public class CategoryFacade extends FacadeImpl<CategoryDTO, CategoryDO, CategoryDTOConverter, CategoryService> {
    public PageInfoDTO<CategoryVO> findCategoryByPage(PageQueryDTO<CategoryReq> req) {
        List<CategoryDO> list = service.queryCategoryByPage(req, ParamConstants.GMT_CREATE, ParamConstants.DESC);
        if (CollectionUtils.isEmpty(list)) {
            return new PageInfoDTO<>(req.getPageNo(), req.getPageSize(), 0, null);
        }
        List<CategoryVO> records = this.converter.do2Vo(list);
        // 根据父ID查询name
        List<Long> ids = list.stream().map(CategoryDO::getParentId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(ids)) {
            Map<Long, String> map = service.findNameByIds(ids);
            records.stream().peek(categoryVO ->
                            categoryVO.setParentName(null != categoryVO.getParentId() ? map.get(categoryVO.getParentId()) : null))
                    .collect(Collectors.toList());
        }
        return new PageInfoDTO<>(req.getPageNo(), req.getPageSize(), service.queryCategoryCount(req.getParam()), records);
    }

    @Transactional
    public Boolean add(CreateCategoryReq req) {
        CategoryDO categoryDO = new CategoryDO();
        categoryDO.setCategoryName(req.getCategoryName());
        if (service.count(categoryDO) > 0) {
            throw new BizException(
                    ResponseError.CATEGORY_EXIST, String.format(ResponseError.CATEGORY_EXIST.getName(), req.getCategoryName()));
        }
        // 新增商品类别
        CategoryDO categoryCreateDo = this.converter.createParam2do(req);
        service.add(categoryCreateDo);
        return true;
    }

    @Transactional
    public Boolean update(CreateCategoryReq req) {
        Long count = getCategoryExist(null, req.getId());
        if (count == 0) {
            throw new BizException(
                    ResponseError.CATEGORY_NOT_EXIST, String.format(ResponseError.CATEGORY_NOT_EXIST.getName(), req.getCategoryName()));
        }
        // 编辑商品类别
        CategoryDO categoryCreateDo = this.converter.createParam2do(req);
        service.update(categoryCreateDo);
        return true;
    }

    private Long getCategoryExist(String categoryName, Long id) {
        CategoryDO categoryDO = new CategoryDO();
        if (StringUtils.isNotBlank(categoryName)) {
            categoryDO.setCategoryName(categoryName);
        }
        if (null != id) {
            categoryDO.setId(id);
        }
        return service.count(categoryDO);
    }

    @Transactional
    public Boolean delete(Long id) {
        Long count = getCategoryExist(null, id);
        if (count == 0) {
            throw new BizException(
                    ResponseError.CATEGORY_NOT_EXIST, String.format(ResponseError.CATEGORY_NOT_EXIST.getName(), id));
        }
        service.removeById(id);
        return true;
    }
}

